<?php
class Admin_AuthController extends Zend_Controller_Action
{
	public function init(){
		$registry = Zend_Registry::getInstance();
		$this->_db = $registry->get('db');
	}
	
	public function homeAction()
    {
	    $storage = new Zend_Auth_Storage_Session();
        $data = $storage->read();
        if(!$data){
            $this->_redirect('admin/auth/login');
        }
       	$this->view->username = $data->username;                


	}
	public function changepasswordAction(){
		Zend_Loader::loadFile("../application/admin/models/Admins.php");
		$client_table = new AML_Model_Admins();
		$this->_helper->layout->setLayout('admin_layout');
		$client_form = new AML_Form_ChangepasswordForm;
		$this->view->form = $client_form;
		$storage = new Zend_Auth_Storage_Session();
        $ses = $storage->read();
		//print_r($ses);
		if($this->getRequest()->isPost()){
            if($client_form->isValid($_POST)){
                
				$data = $client_form->getValues();
				if($data['admin_password'] != $data['conreg_pass']){
					$this->view->error = "<strong>Error(s):</strong><br>Your new passwords are not matched";
				}else{			
					$data['old_pass'] = md5($data['old_pass']);
					
					$select = $client_table->select('admin_id')->where('admin_id="'.$ses->admin_id.'" AND admin_password="'.$data['old_pass'].'"');
					$rowset = $client_table->fetchRow($select);
					
					if($rowset->admin_id != ""){
											
						unset($data['conreg_pass']);
						unset($data['old_pass']);
						$where = 'admin_id = '. $rowset->admin_id;
						$data['admin_password']=md5($data['admin_password']);
						$client_table->update($data,$where);
						$this->view->success = "Password changed successfully";
						
					}else{
						$this->view->error = "<strong>Error(s):</strong><br>Your current password is not correct";
					}
					
				}
            }
        }
	}
	 

	public function loginAction()
    {
		$storage = new Zend_Auth_Storage_Session();
        $data = $storage->read();
        if($data){
            $this->_redirect('admin/index');
        }
		
		$this->_helper->layout->setLayout('layout_login');
		Zend_Loader::loadFile("../application/admin/models/Admins.php");
		Zend_Loader::loadFile("../application/admin/forms/LoginForm.php");
		$users = new AML_Model_Admins();
        $form = new AML_Form_LoginForm();
        $this->view->form = $form;
        if($this->getRequest()->isPost()){
            if($form->isValid($_POST)){
                $data = $form->getValues();
                $auth = Zend_Auth::getInstance();
                //$authAdapter = new Zend_Auth_Adapter_DbTable($users->getAdapter(),'aml_admin');
                $authAdapter = new Zend_Auth_Adapter_DbTable($this->_db,'aml_admin');
               
			    $authAdapter->setIdentityColumn('admin_username')
                            ->setCredentialColumn('admin_password');
               
			    $authAdapter->setIdentity($data['admin_username'])
                            ->setCredential(md5($data['admin_password']));
                $result = $auth->authenticate($authAdapter);
                
				if($result->isValid()){
                    $storage = new Zend_Auth_Storage_Session();
                    $storage->write($authAdapter->getResultRowObject());
                    $this->_redirect('admin/index');
                } else {
                    $this->view->errorMessage = "<span class='error'>Invalid username or password. Please try again.</span>";
                }      
				   
            }
        }
    } 
	
	public function signupAction()
    {
		$this->_helper->layout->setLayout('layout_login');
        $users = new AML_Model_Admins();
        $form = new AML_Form_RegistrationForm();
        $this->view->form=$form;
        if($this->getRequest()->isPost()){
            if($form->isValid($_POST)){
                $data = $form->getValues();
                if($data['admin_password'] != $data['admin_confirmPassword']){
                    $this->view->errorMessage = "Password and confirm password don't match.";
                    return;
                }
                if($users->checkUnique($data['admin_username'])){
                    $this->view->errorMessage = "Name already taken. Please choose another one.";
                    return;
                }
                unset($data['admin_confirmPassword']);
				$data['admin_password']=md5($data['admin_password']);
                $users->insert($data);
                $this->_redirect('auth/login');
            }
        }
    }
    
    public function logoutAction()
    {
        $storage = new Zend_Auth_Storage_Session();
        $storage->clear();
        $this->_redirect('admin/auth/login');
    }
	
	public function indexAction()
    {
        $storage = new Zend_Auth_Storage_Session();
        $data = $storage->read();
        if(!$data){
            $this->_redirect('auth/login');
        }
       	$this->view->username = $data->username;                
    }

}
